# -*- coding: utf-8 -*-

import clr
import re
import Misuzilla.Crigate

from System                          import *
from System.Collections.Generic      import *
from System.Diagnostics              import Trace
from System.Runtime.InteropServices  import Marshal
from Misuzilla.Crigate               import ConsoleHandler, Console, Context
from Misuzilla.Crigate.Configuration import IConfiguration, ICustomConfiguration, ConfigurationPropertyInfo
from Misuzilla.Crigate.Scripting     import DLRIntegrationHandler, DLRBasicConfiguration, DLRContextHelper
from Misuzilla.Net.Irc import *

class OutlookConsole(object):
	def __init__(self, channel_name, session):
		self.channel_name = channel_name
		self.session = session
	
	def setup(self):
		# Unload
		CurrentSession.HandlerLoader.GetHandler[DLRIntegrationHandler]().BeforeUnload += self.onBeforeUnload
		
		# Console
		self.console = Misuzilla.Crigate.Console()
		self.console.Initialize(self.session)
		self.console.Attach(self.channel_name, DLRContextHelper.Wrap(CurrentSession, "OutlookContext", OutlookContext))

	def cleanup(self):
		# Console
		self.console.Detach()

	#region Events
	def onBeforeUnload(self, sender, args):
		self.cleanup()
	#endregion

class OutlookContext(Context):
	#region Context
	def Initialize(self):
		self.connect("Outlook.Application")

	def GetCommands(self):
		d = Dictionary[String, String](Context.GetCommands(self))
		for m in [self.play, self.pause, self.next, self.prev, self.tracks, self.repeat, self.shuffle, self.check, self.uncheck]:
			d[m.__name__] = m.__doc__
		return d

	def OnUninitialize(self):
		self.disconnect()
	#endregion

	#region COM Connection
	def connect(self, progId):
		t = Type.GetTypeFromProgID(progId)
		self.com_object = Activator.CreateInstance(t)
		self.onCOMObjectConnected()

	def disconnect(self):
		self.onCOMObjectDisconnected()
		Marshal.ReleaseComObject(self.com_object)
		self.com_object = None
	#endregion

	#region COM Initialize
	def onCOMObjectConnected(self):
		self.com_object.NewMailEx += self.onNewMailEx
		self.com_object.Reminder += self.onReminder

	def onCOMObjectDisconnected(self):
		self.com_object.NewMailEx -= self.onNewMailEx
		self.com_object.Reminder -= self.onReminder
	#endregion

	#region Events
	def onNewMailEx(self, entryIDCollection):
		Trace.WriteLine("onNewMailEx")
		for entryID in entryIDCollection.split(','):
			mail = self.com_object.Session.GetItemFromID(entryID)
			self.send_notice("Inbox", ("%s | %s" % (mail.SentOnBehalfOfName, mail.Subject)))

	def onReminder(self, item):
		Trace.WriteLine("onReminder")
		self.send_notice("Reminder", ("%s [%s] | %s～%s" % (item.Subject, item.Location, item.Start, item.End)))
	#endregion

	def send_notice(self, sender, message):
		for line in message.split("\n"):
			notice_msg = NoticeMessage(self.Console.ConsoleChannelName, line)
			notice_msg.SenderNick = sender
			self.CurrentSession.Send(notice_msg)

outlook_console = OutlookConsole("#Outlook", CurrentSession)
outlook_console.setup()
